package com.lyrcsoft.springboottransactional.controller;

import com.lyrcsoft.springboottransactional.domain.User;
import com.lyrcsoft.springboottransactional.service.UserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author fujc2dev@126.com
 * @date 2024-12-11
 */
@RequestMapping("/users")
@RestController
public class UsersController {
    // Bean的注入
    @Resource
    private UserService usersService;

    @PostMapping("/add")
    public String addUsers(@RequestBody User user) {
        this.updateUser(user);
        return  "ok";
    }

    @Transactional(rollbackFor = Exception.class)
    public String updateUser(User user) {
        usersService.updateById(user);
        user.setId(null);
        int i = 1/0;
        return  usersService.addUsers(user) ? "添加成功" : "添加失败";
    }
}

